package com.briup.jz.service.impl;

import com.briup.jz.bean.BaseRole;
import com.briup.jz.bean.BaseRoleExample;
import com.briup.jz.dao.BaseRoleMapper;
import com.briup.jz.service.IBaseRoleService;
import com.briup.jz.utils.CustomerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BaseRoleServiceImpl implements IBaseRoleService {
    @Autowired
    private BaseRoleMapper baseRoleMapper;
    @Override
    public void saveOrUpdate(BaseRole baseRole) throws CustomerException {
        if(baseRole.getId()==null){
            baseRoleMapper.insert(baseRole);
        }else{
            BaseRole baseRole1 = baseRoleMapper.selectByPrimaryKey(baseRole.getId());
            if (baseRole1==null){
                throw new CustomerException("该角色不存在");
            }
            baseRoleMapper.updateByPrimaryKey(baseRole);
        }
    }

    @Override
    public List<BaseRole> query() {
        BaseRoleExample example = new BaseRoleExample();
        return baseRoleMapper.selectByExample(example);
    }

    @Override
    public void deleteById(long id) throws CustomerException {
        BaseRole baseRole1 = baseRoleMapper.selectByPrimaryKey(id);
        if (baseRole1==null){
            throw new CustomerException("该角色不存在");
        }
        baseRoleMapper.deleteByPrimaryKey(id);
    }
}
